﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Danger
{
    public class DetailsDangerModel : SearchDangerInfo
    {
        private riskanalysisEntities entities = new riskanalysisEntities();

        public DetailsDangerModel(int dangerId)
        {
            Utils.GetFrequencyList();
            Utils.GetAffectedList();
            Utils.GetControlExistenceList();
            Utils.GetSeverityList();
            var query = from d in entities.dangers
                        where d.dangerId == dangerId
                        select new
                        {
                            DangerId = d.dangerId,
                            Management = d.activity.area.process.management.name,
                            Process = d.activity.area.process.name,
                            Area = d.activity.area.name,
                            Activity = d.activity.name,
                            Causes = d.causes,
                            Consecuences = d.consecuences,
                            Controls = d.controlExistence,
                            Danger = d.description,
                            EspecificUbication = d.ubication,
                            ExistingControls = d.controls,
                            ExpirationDate = d.expirationDate,
                            ExposedNumber = d.exposed,
                            Frequence = d.frecuence,
                            KFactor = d.kFactor,
                            OperativeCondition = d.operativeCondition,
                            PosibleControls = d.posibleControls,
                            Risk = d.risk.name,
                            Severity = d.severity,
                            Status = d.status,
                            AffectedTarget = d.affectedTarget,
                            Inversion = d.inversion
                        };

            var theDanger = query.FirstOrDefault();

            if (theDanger == null)
                throw new DetailsDangerException("La actividad especificada no existe");
            DangerId = theDanger.DangerId;
            Management = theDanger.Management;
            Process = theDanger.Process;
            Area = theDanger.Area;
            Activity = theDanger.Activity;
            Causes = theDanger.Causes.Replace("\n", "<br />");
            Consecuences = theDanger.Consecuences.Replace("\n", "<br />");
            Controls = Utils.ControlExistance[theDanger.Controls - 1];
            Danger = theDanger.Danger.Replace("\n", "<br />");
            EspecificUbication = theDanger.EspecificUbication.Replace("\n", "<br />");
            ExistingControls = theDanger.ExistingControls.Replace("\n", "<br />");
            ExpirationDate = theDanger.ExpirationDate.ToString("d", Utils.culture);
            ExposedNumber = Utils.Affecteds[theDanger.ExposedNumber - 1];
            Frequence = Utils.Frequences[theDanger.Frequence - 1];
            Utils.GetKValuesList();
            KFactor = Utils.KValuesDictionary[theDanger.KFactor];
            OperativeCondition = theDanger.OperativeCondition.Equals("N") ? "Normal" : "Anormal";
            AffectedTarget = theDanger.AffectedTarget.Equals("H") ? "Humano" : "Infraestructura";
            PosibleControls = theDanger.PosibleControls.Replace("\n", "<br />");
            Risk = theDanger.Risk;
            Severity = Utils.Severitys[theDanger.Severity - 1];
            Status = theDanger.Status.ToString();
            Inversion = theDanger.Inversion.ToString();
            CalculateRisk((from d in entities.dangers
                           where d.dangerId == dangerId
                           select d).FirstOrDefault());            
        }

        public void DeleteDanger()
        {
            var dangerList = from d in entities.dangers
                            where d.dangerId == DangerId
                            select d;

            if (dangerList.Count() <= 0)
                throw new DetailsDangerException("El peligro especificado no fue encontrado");

            entities.dangers.DeleteObject(dangerList.First());
            entities.SaveChanges();
        }
    }
}